#ifndef _MAINWINDOW_H
#define _MAINWINDOW_H

#include <wx/wx.h>
#include <wx/image.h>
#include "player.h"
#include "blackjack.h"
#include "networking.h"

const int ID_SAY_HELLO = 1000;
const int ID_QUIT_GAME = 1001;
const int ID_START_NETWORKING = 1002;
const int ID_START_CLIENT = 1003;
const int ID_WAR_START = 1004;

class EventFrame;

class CardField : public wxWindow //Drawing frame
{
 public:
  CardField(EventFrame* parent); //Creates a new CardField class
  
  void OnPaint(wxPaintEvent& event); // called with the screen's refresh rate, used to have images that stay
  
  void SplashOff(); //Sets showSplash to 0
  //Preconditions: the pokercard image is being redrawn when it shouldn't be 
  //Postconditions: showSplash is set to 0 and the pokecard image shouldnt be redrawn anymore
 private:
  wxBitmap bitmap; 
  EventFrame* daddy; //pointer to the parent
  bool showSplash; //affects some functions in OnPaint
  //*No longer needed*//bool moveFix; // used to get CardField off of our favorite buttons only once to not cause the window to jump around whenever the screen redraws
  DECLARE_EVENT_TABLE()

    };


class EventFrame : public wxFrame
{
 public:

  EventFrame();

  //  void DrawButtons(); No longer used
  void NewGame(wxCommandEvent& event);
  //Preconditions: The Blackjack button is pushed by the user and no function is currently running
  //Postconditions: Blackjack.Play() is run
  void NewOnlineGame(wxCommandEvent& event);
  //Preconditions: The Blackjack Server button was pushed by the user and no function is currently running
  //Postconditions: Blackjack.PlayOnline() is run
  void StartClient(wxCommandEvent& event);
  //Preconditions: The Client button was pushed by the user and no function is currently running
  //Postconditions: a loop is started which is used to interact with a server
  void StartWar(wxCommandEvent& event);
  //Preconditions: The war button is pushed
  //Postconditions: A game of war is started

  void QuitGame(wxCommandEvent& event);
  //Preconditions: The user clicked file>Quit or help>there's no helping you
  //Postconditions: The wxApp is closed, terminating the program

  void OnPaint(wxPaintEvent &event);
 private:
  CardField* field;
  DECLARE_EVENT_TABLE()
    };


class MainWindow: public wxApp
{
public:
  virtual bool OnInit();
private:
  EventFrame* frame;
};


BEGIN_EVENT_TABLE(EventFrame, wxFrame)
   EVT_BUTTON(ID_SAY_HELLO, EventFrame::NewGame)
   EVT_BUTTON(ID_START_NETWORKING, EventFrame::NewOnlineGame)
   EVT_BUTTON(ID_START_CLIENT, EventFrame::StartClient)
   EVT_BUTTON(ID_WAR_START, EventFrame::StartWar)
   EVT_MENU(ID_QUIT_GAME, EventFrame::QuitGame)
   EVT_PAINT(EventFrame::OnPaint)
END_EVENT_TABLE()

BEGIN_EVENT_TABLE(CardField, wxWindow)
   EVT_PAINT(CardField::OnPaint)
END_EVENT_TABLE()
   /*
void EventFrame::OnPaint(wxPaintEvent &event) {
  wxPaintDC dc(this);
  PrepareDC(dc);
  dc.DrawBitmap(bitmap, 0,0, true);
}
   */



   CardField::CardField(EventFrame* parent) : wxWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxFULL_REPAINT_ON_RESIZE)
{
  //wxSize size = GetSize();
  //SetSize(0,20,size.GetWidth(), size.GetHeight() - 20);
  showSplash = true;
  wxInitAllImageHandlers();
  //  moveFix = true; no longer needed
  daddy = parent;
}

void CardField::OnPaint(wxPaintEvent &event)
{
  //if (moveFix == true){
    //Move(0,30); 
    //moveFix = false; //no longer needed
    //daddy->DrawButtons(); no longer needed
  //}
  wxPaintDC dc(this);
  //  PrepareDC(dc);
  wxSize size = GetSize();
  dc.Clear();
  dc.SetBrush(*wxTRANSPARENT_BRUSH);
  //dc.DrawEllipse(0,0,size.GetWidth(), size.GetHeight());
  //dc.DrawBitmap(bitmap, 0, 0, true);
  
  wxImage awesomeface(wxT("awefac.png"));
  //awesomeface.wxInitAllImageHandlers();
  //awesomeface.LoadFile(wxT("awefac.jpg"));
  wxBitmap awebit(awesomeface);
  //wxScreenDC awedc;
  //awedc.StartDrawingOnTop(field); //We need to get this line of code working to have permanent images (I think)
  //PrepareDC(awedc);
  if (showSplash == true)
    dc.DrawBitmap(awebit, 0, 0, true);
  
}

void CardField::SplashOff()
{
  showSplash = false;
}


EventFrame::EventFrame() : wxFrame(NULL, -1, wxT("PokerCards"))
{
  SetSize(150,150, 400, 250);
  wxMenu* menu = new wxMenu();
  menu->Append(ID_QUIT_GAME, wxT("Quit"));

  wxMenu* menu2 = new wxMenu();
  menu2->Append(ID_QUIT_GAME, wxT("There's no helping you"));

  wxMenuBar* menuBar = new wxMenuBar();
  SetMenuBar(menuBar);
  menuBar->Append(menu, wxT("File"));
  menuBar->Append(menu2, wxT("Help"));

  field = new CardField(this);

  wxButton* hello_button = new wxButton(this, ID_SAY_HELLO, wxT("BlackJack"));
  wxButton* online_button =  new wxButton(this, ID_START_NETWORKING, wxT("BlackJack Server"));
  wxButton* client_button = new wxButton(this, ID_START_CLIENT, wxT("Client"));
  wxButton* war_button = new wxButton(this, ID_WAR_START, wxT("Play War"));
  wxBoxSizer* button_sizer = new wxBoxSizer(wxHORIZONTAL);

  button_sizer->Add(hello_button);
  button_sizer->Add(online_button);
  button_sizer->Add(client_button);
  button_sizer->Add(war_button);

  wxBoxSizer* frame_sizer = new wxBoxSizer(wxVERTICAL);
  frame_sizer->Add(button_sizer, 0, wxALIGN_CENTER);
  frame_sizer->Add(field,1, wxGROW);
  
  
  SetAutoLayout(true);

  SetSizer(frame_sizer);

  /*
  wxButton* hello_button = new wxButton(this, ID_SAY_HELLO, wxT("BlackJack"));
  
  wxBoxSizer* button_sizer = new wxBoxSizer(wxHORIZONTAL);
  button_sizer->Add(hello_button);
  
  */
  //SetAutoLayout(true);
  
  }

/* No longer used
void EventFrame::DrawButtons()
{
  wxButton* hello_button = new wxButton(this, ID_SAY_HELLO, wxT("BlackJack"));
  wxButton* online_button =  new wxButton(this, ID_START_NETWORKING, wxT("BlackJack Server"));
  wxBoxSizer* button_sizer = new wxBoxSizer(wxHORIZONTAL);

  button_sizer->Add(online_button);
  button_sizer->Add(hello_button);
  SetSizer(button_sizer);
  
  
  SetAutoLayout(true);


}

*/



void EventFrame::OnPaint(wxPaintEvent& event)
{

}

void EventFrame::NewGame(wxCommandEvent& event)
{
  //std::cout << "YOU PUSHED THE BUTTON!!!  WHOOPIDIE-DO!!\n";

  wxTextEntryDialog* dialog = new wxTextEntryDialog(this, wxT("How many players?"));
  dialog->ShowModal();
  int x = atoi(dialog->GetValue().char_str());
  dialog->Destroy();
  if (x < 1 || x > 10)
    {
      wxMessageDialog* dialog = new wxMessageDialog(this, wxT("Please choose a number of players that is between 1 and 10"));
      dialog->ShowModal();
      dialog->Destroy();
      return;
    }



  /*
  wxInitAllImageHandlers();
  wxImage awesomeface(wxT("awefac.png"));
  //awesomeface.wxInitAllImageHandlers();
  //awesomeface.LoadFile(wxT("awefac.jpg"));
  wxBitmap awebit(awesomeface);
  wxScreenDC awedc;
  awedc.StartDrawingOnTop(field); //We need to get this line of code working to have permanent images (I think)
  //PrepareDC(awedc);
  awedc.DrawBitmap(awebit, 0, 0, true); //Well it's working now.  Don't know why it wasn't working earlier
  */
  
  Blackjack bj = Blackjack(x);
  bj.Play();

}

void EventFrame::StartClient(wxCommandEvent& event)
{
      bool client = true;
      wxString exit = wxT("exit");

      wxTextEntryDialog* dialog2 = new wxTextEntryDialog(this, wxT("IP address of server?"));
      dialog2->ShowModal();
      wxString temp = dialog2->GetValue();
      char* ip = new char[temp.Len() + 1];
      copy(temp.begin(), temp.end(), ip);
      ip[temp.Len()] = '\0';
      dialog2->Destroy();

      while (client)
	{
      
	  const char* message;
	  char* reply;
	  string temp2;
	  network gameClient;
	  /*
	  cout << "Enter message: ";
	  cin >> temp2;
	  message = temp2.c_str();
	  reply = gameClient.runClient(ip, message);
	  cout << reply << endl;
	  */
	  wxTextEntryDialog* dialog3 = new wxTextEntryDialog(this, wxT("Message to send to server? (type 'exit' to close)"));
	  dialog3->ShowModal();
	  if (dialog3->GetValue().Cmp(exit) == 0)
	    client = false;
	  else
	    {
	      message = dialog3->GetValue().mb_str();
	      reply = gameClient.runClient(ip, message);
	      wxString temp3(reply, wxConvUTF8);
	      wxMessageDialog* dialog4 = new wxMessageDialog(this, temp3);
	      dialog4->ShowModal();
	      dialog4->Destroy();
	    }


	  //}
	  //delete[] ip;
	  
	}
      delete[]ip;

}
void EventFrame::NewOnlineGame(wxCommandEvent& event)
{

  /*  unneccessary since it only works when there are two players

wxTextEntryDialog* dialog = new wxTextEntryDialog(this, wxT("How many players?"));
  dialog->ShowModal();
  int x = atoi(dialog->GetValue().char_str());
  dialog->Destroy();
  if (x < 1 || x > 10)
    {
      wxMessageDialog* dialog = new wxMessageDialog(this, wxT("Please choose a number of players that is between 1 and 10"));
      dialog->ShowModal();
      dialog->Destroy();
      return;
    }
  */

  Blackjack bj = Blackjack(2);
  bj.PlayOnline();
}

void EventFrame::QuitGame(wxCommandEvent& event)
{
  Close();
  //  field->SplashOff();  
  //field->Move(0,50);

  /*
    Now covered by eventframe::eventframe
  wxButton* hello_button = new wxButton(this, ID_SAY_HELLO, wxT("BlackJack"));
  
  wxGridSizer* button_sizer = new wxGridSizer(6, 4, 10, 10);//wxHORIZONTAL);
  button_sizer->Add(hello_button);
  */


  //Close(); //Gets frame or window to close.
  //std::cout << "If this wasn't such a shitty application, this would close the program!\n";
}


//DECLARE_APP(MainWindow) //Declared in MainWindow.cpp now

//IMPLEMENT_APP(MainWindow) //samesies

void EventFrame::StartWar(wxCommandEvent& event)
{

  srand(time(0)); // part of randomization
  


  Deck warDeck;
  //warDeck.PrintDeck();
  cout << "Welcome to War, press Enter to draw your next card." << endl;
  warDeck.Shuffle();
  Deck playerOne(0), playerTwo(0);
  Deck temporaryDeck(0);
  
  int i = 1; // i is a counter, adds cards 1-26 from deck to player 1's deck
  while (i < 27)
    {
      playerOne.InsertAtBottom(warDeck.DrawCard());
      i++;
    }
  i = 1;
  while(i < 27)
    {
      playerTwo.InsertAtBottom(warDeck.DrawCard());
      i++;
    }

  cout << "Now Starting the Game..." << endl;


  Cards* pOneCard,* pTwoCard;
  while(playerOne.Size() > 0 && playerTwo.Size() > 0){
    pOneCard = playerOne.DrawCard();
    pTwoCard = playerTwo.DrawCard();
    cout << "You drew: " << pOneCard->GetSuit() <<  " " <<pOneCard->GetValue() << " and He Drew: " << pTwoCard->GetSuit() << " " << pTwoCard->GetValue() << endl;
    if(pOneCard->GetValue() > pTwoCard->GetValue()) // if player 1's card has a higher value, add both cards to the bottom of his deck
      {
	playerOne.InsertAtBottom(pOneCard);
	playerOne.InsertAtBottom(pTwoCard);
	cout << "Both cards added to your deck!" << endl;

      }
    else if (pOneCard->GetValue() < pTwoCard->GetValue())
      {
	playerTwo.InsertAtBottom(pTwoCard);
	playerTwo.InsertAtBottom(pOneCard);
	cout << "Both cards added to his deck..." << endl;
      }
    else 
      {
	cout << "War!" << endl;

	//cout << playerOne.Size() << endl;
	//cout << playerTwo.Size() << endl;

	//cin; // use for autoplay
	cin.get(); //use for manual play
	bool war = true;
	while(war){
	temporaryDeck.InsertAtBottom(pOneCard);
	temporaryDeck.InsertAtBottom(pTwoCard);

	if (playerOne.Size() > 2 && playerTwo.Size()> 2) // checks if players have more cards to play
	  {
	    pOneCard = playerOne.DrawCard();
	    temporaryDeck.InsertAtBottom(pOneCard);
	    pTwoCard = playerTwo.DrawCard();
	    temporaryDeck.InsertAtBottom(pTwoCard);
	    cout << "You drew: " << pOneCard->GetSuit() << " " << pOneCard->GetValue() << " and He Drew: " << pOneCard->GetSuit() << " " << pTwoCard->GetValue() << endl;
	    
	    pOneCard = playerOne.DrawCard();
	    temporaryDeck.InsertAtBottom(pOneCard);
	    pTwoCard = playerTwo.DrawCard();
	    temporaryDeck.InsertAtBottom(pTwoCard);
	    cout << "You drew: " << pOneCard->GetSuit() <<  " " <<pOneCard->GetValue() << " and He Drew: " << pOneCard->GetSuit() <<  " " <<pTwoCard->GetValue() << endl;
	    
	    pOneCard = playerOne.DrawCard();
	    temporaryDeck.InsertAtBottom(pOneCard);
	    pTwoCard = playerTwo.DrawCard();
	    temporaryDeck.InsertAtBottom(pTwoCard);
	    cout << "You drew: " << pOneCard->GetSuit() <<  " " <<pOneCard->GetValue() << " and He Drew: " << pOneCard->GetSuit() <<  " " <<pTwoCard->GetValue() << endl;
	  
	    if (pOneCard->GetValue() > pTwoCard->GetValue())
	      {
		cout << "You won the war!" << endl;
		int c = 0;
		while (c < temporaryDeck.Size()){
		playerOne.InsertAtBottom(temporaryDeck.DrawCard());
		c++;
		}
		war = false;
	      }
	    else if (pOneCard->GetValue() < pTwoCard->GetValue())
	      {
		cout << "He won the war..." << endl;
		int c = 0;
		while (c < temporaryDeck.Size()){
		playerTwo.InsertAtBottom(temporaryDeck.DrawCard());
		c++;
		}
		war = false;
	      }
	    //else war = false;
	    //delete temporaryDeck;
	  }
	}
	
	
	
      }
    //cin; //use for autoplay
    cin.get(); // use for manual play
  }
  
  if (playerOne.Size() > 0 )
    {
      cout << "Congrats, you won the war." << endl;
    }
  else
    {
      cout << "Sorry, you lost..." << endl;
    }
  //delete temporaryDeck;


}

#endif
